<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="openAssignPermissionDialog(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="query.page"
                :page-sizes="[3, 5, 10, 15]"
                :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 新增角色对话框 -->
    <el-dialog
      :title="roleForm.id ? '编辑角色' : '新增角色'"
      :visible="showDialog"
      @close="handleDialogClose"
    >
      <el-form
        ref="roleFormRef"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog = false">取消</el-button>
          <el-button size="small" type="primary" @click="submitForm"
            >确定</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>

    <AssignPermission
      ref="permission"
      :show-assign-dialog.sync="showAssignDialog"
    ></AssignPermission>
  </div>
</template>

<script>
import { getRoleListApi, deleteRoleApi, addRoleApi, getRoleDetailApi, updateRoleApi } from '@/api/setting'
import AssignPermission from './components/assign-permission.vue'

export default {
  components: {
    AssignPermission
  },
  data () {
    return {
      roleList: [],
      query: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showAssignDialog: false
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { rows, total } = await getRoleListApi(this.query)
      // console.log(data)
      this.roleList = rows
      this.total = total
    },
    handleSizeChange (newPageSize) {
      this.query.pagesize = newPageSize
      this.getRoleList(this.query)
    },
    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.getRoleList(this.query)
    },
    async deleteRole (id) {
      try {
        await this.$confirm('确定要删除该角色吗', '删除提示')
        await deleteRoleApi(id)
        this.$message.success('删除成功')
        this.getRoleList()
      } catch (error) {
        console.log(error)
        this.$message.error('删除失败')
      }
    },
    submitForm () {
      this.$refs.roleFormRef.validate(async valid => {
        if (!valid) return
        if (this.roleForm.id) {
          // 编辑
          await updateRoleApi(this.roleForm)
        } else {
          // 添加
          await addRoleApi(this.roleForm)
        }
        this.$message.success('操作成功!')
        this.getRoleList()
        this.showDialog = false
      })
    },
    handleDialogClose () {
      this.$refs.roleFormRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
      this.showDialog = false
    },
    async editRole (id) {
      this.showDialog = true
      const detail = await getRoleDetailApi(id)
      this.roleForm = detail
    },
    openAssignPermissionDialog (id) {
      this.showAssignDialog = true
      this.$refs.permission.getRoleDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
